package com.xjh.t.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xjh.t.common.Constast;
import com.xjh.t.common.DataGridView;
import com.xjh.t.domain.*;
import com.xjh.t.service.*;
import com.xjh.t.vo.FirstOrderInfoVo;
import com.xjh.t.vo.OrderInfoVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author jobob
 * @since 2020-05-05
 */
@RestController
@RequestMapping("/firstOrderinfo")
public class FirstOrderInfoController {

    @Autowired
    private IFirstOrderInfoService firstOrderInfoService;

    @Autowired
    private IUserService userService;

    @Autowired
    private IGroupInfoService groupInfoService;

    @Autowired
    private IUserInfoService userInfoService;
    @Autowired
    private IAreaInfoService areaInfoService;

    @RequestMapping("getFirstList")
    public DataGridView getFirstList(FirstOrderInfoVo vo) throws Exception {
        IPage<FirstOrderInfo> page = new Page<>(vo.getPage(), vo.getLimit());
        QueryWrapper<FirstOrderInfo> queryWrapper = new QueryWrapper<>();

        if(StringUtils.isNotBlank(vo.getOutPutCenter()))
        {
            List<String> manufacture = Arrays.asList(vo.getOutPutCenter().split(","));
            queryWrapper.in(manufacture.size() > 0, "fo_manufacturer_name", manufacture);
        }

        if(StringUtils.isNotBlank(vo.getGroupName()))
        {
            List<Integer> gpids = new ArrayList<>();
            QueryWrapper<GroupInfo> groupInfoQueryWrapper = new QueryWrapper<>();
            groupInfoQueryWrapper.like("gp_name",vo.getGroupName());
            List<GroupInfo> groupInfoList = this.groupInfoService.list(groupInfoQueryWrapper);
            for(GroupInfo groupInfo:groupInfoList)
                gpids.add(groupInfo.getGpId().intValue());
            queryWrapper.in(gpids.size() > 0,"gp_id",gpids);
        }

        if(StringUtils.isNotBlank(vo.getAuthorUser()))
        {
            List<Integer> userids = new ArrayList<>();
            QueryWrapper<User> userQueryWrapper = new QueryWrapper<>();
            userQueryWrapper.like("name",vo.getAuthorUser());
            List<User> userList = this.userService.list(userQueryWrapper);
            for(User user:userList)
                userids.add(user.getId());
            queryWrapper.in(userids.size() > 0,"ur_confirm_id",userids);
        }

        // queryWrapper.eq("or_id","18014398509481985");

        if (vo.getStartTime() == null) {
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");//注意月份是MM
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

            String datestr = simpleDateFormat.format(new Date());
            vo.setStartTime(sdf.parse(datestr + " 00:00:00"));
            vo.setEndTime(sdf.parse(datestr + " 23:59:59"));

        }
        queryWrapper.ge(vo.getStartTime() != null, "fo_date", vo.getStartTime());
        queryWrapper.le(vo.getEndTime() != null, "fo_date", vo.getEndTime());

        queryWrapper.orderByDesc("fo_date");

//        String url1 = "jdbc:mysql://127.0.0.1:3306/scfy?serverTimezone=UTC&useUnicode=true&useSSL=false&characterEncoding=utf8";
//
//        openSqlSession(url1);
//        if(orderInfoMapper == null)
//            orderInfoMapper = sqlsession.getMapper(OrderInfoMapper.class);

        this.firstOrderInfoService.page(page, queryWrapper);
        //  System.out.println(page.getTotal());
        //this.orderInfoService.page(page,queryWrapper);

        List<UserInfo> userInfos = this.userInfoService.list();
        Map<Integer, String> userAccount = userInfos.stream().collect(Collectors.toMap(UserInfo::getUrId, UserInfo::getUrPhonenumber));

        List<User> users = this.userService.list();
        Map<Integer, String> usermap = users.stream().collect(Collectors.toMap(User::getId, User::getName));

        List<GroupInfo> groupInfos = this.groupInfoService.list();
        Map<Long,String> groupMap = groupInfos.stream().collect(Collectors.toMap(GroupInfo::getGpId, GroupInfo::getGpName));

        List<FirstOrderInfo> orderInfos = page.getRecords();
        List<FirstOrderInfoVo> result = new ArrayList<>();

        for(FirstOrderInfo firstOrderInfo:orderInfos)
        {
            FirstOrderInfoVo orderInfoVo = new FirstOrderInfoVo();
            BeanUtils.copyProperties(firstOrderInfo,orderInfoVo);

            orderInfoVo.setAuthorUser(usermap.get(orderInfoVo.getUrConfirmId()));

            orderInfoVo.setFounderPhone(userAccount.get(orderInfoVo.getUrFounderId()));
            orderInfoVo.setGroupName(groupMap.get(orderInfoVo.getGpId().longValue()));

            GroupInfo groupInfo = this.groupInfoService.getById(orderInfoVo.getGpId());
            String addr = "";
            AreaInfo areaInfo = this.areaInfoService.getById(groupInfo.getGpZone());
            while (areaInfo != null && areaInfo.getAaLevel() != Constast.POLICY_TOP_LEVEL)
            {
                addr = areaInfo.getAaAreaname() + " " + addr;
                areaInfo = this.areaInfoService.getById(areaInfo.getAaParentid());
            }
            if(areaInfo == null)
            {
                System.out.println(groupInfo.getGpId() + "," + groupInfo.getGpName());
            }
            else
                addr = areaInfo.getAaAreaname() + " " + addr;

            orderInfoVo.setGpPolicyAddr(addr);

            result.add(orderInfoVo);
        }

        return new DataGridView(page.getTotal(),result);
    }
}

